
#include "lib_image.h"
//****************************************************************

#if 1
#define  CONFIG_PHOTO_PATH  "/home/photo/"
#elif 1
#define  CONFIG_PHOTO_PATH  "/media/root/"
#endif


#define  CONFIG_LCD_EDP
//#define  CONFIG_LCD_HDMI 
//****************************************************************

static  pthread_t     lib_image_thread;
static  pthread_t     lib_time_thread;
static  int           lib_image_index = 1;
static  file_list_t*  lib_image_list  = NULL;

//****************************************************************
int lib_image_show(int lcd_type)
{
	int ret = -1;
	int total = 0;

	char filename[1024] = {0};

	//****************************************************************
	
	memset(filename,0,sizeof(filename));
	//****************************************************************

#if 1
	total = file_list_get_filenum(lib_image_list);
	if(total< 2){
		return -1;
	}

	lib_image_index = photo_para_get_playindex();
	lib_image_index++;
	if(lib_image_index >= total)
	{
		lib_image_index = 1;
	}
	//****************************************************************
	ret = file_list_get_filename(lib_image_list,filename,lib_image_index);
	//****************************************************************
#endif
	
	if(ret != 0){
		return -1;
	}
	//****************************************************************
	//log_i("i:t:n=%d:%d:%s\n",lib_image_index,total,filename);
	//****************************************************************
	if(!__Valid_PhotoFileType(filename)){
		return -1;
	}
	//****************************************************************
	if(!is_file_exit(filename)){
		return -1;
	}

	photo_para_set_playindex(lib_image_index);
	system_para_save();
	//****************************************************************
#ifdef CONFIG_LCD_HDMI 
	if(lcd_type == 0)
	{
		decode_and_show_jpeg_0(filename);
	}
#endif

#ifdef CONFIG_LCD_EDP
	if(lcd_type == 1)
	{
		decode_and_show_jpeg_1(filename);
	}
#endif
	//****************************************************************

	return 0;
}


//****************************************************************
void* lib_image_show_task(void *arg)
{
	int ret = -1;
	struct timeval timer;	
	
	while(1)
	{
		//****************************************************************
		ret = lib_image_show(0);
		if(ret != 0){
			continue;
		}
		//****************************************************************
		ret = lib_image_show(1);
		if(ret != 0){
			continue;
		}
		//****************************************************************

		//****************************************************************
		timer.tv_sec  = 6;
		timer.tv_usec = 0;
		
		switch(select(0, NULL, NULL, NULL, &timer)) 
		{
		case -1:  
			log_e("Error!\n");
			break;
		case 0: 
			break;
		default:break;
		}
		//****************************************************************

		usleep(1000);

		//****************************************************************
	}
}

void* lib_image_msg_task(void *arg)
{
	int ret = -1;
	msg_box_t msg;

	while(1)
	{
		usleep(1000);
		
		continue;	
		//****************************************************************
		ret = message_packet_recv(&msg.msg_cmd,&msg.msg_data0,&msg.msg_data1);
		if(ret != 0){
			continue;	
		}
		//****************************************************************
		switch(msg.msg_cmd){
		case MSG_KEY_UP:
		case MSG_KEY_PREV:
		case MSG_KEY_LEFT:
		{
			ret = lib_image_show(0);
			ret = lib_image_show(1);
			break;
		}
		case MSG_KEY_DOWN:
		case MSG_KEY_NEXT:
		case MSG_KEY_RIGHT:
		{
			ret = lib_image_show(0);
			ret = lib_image_show(1);
			break;
		}	
		default:break;
		}
		//****************************************************************


		//****************************************************************
	}
}

//****************************************************************

int lib_image_init(void)
{
	int ret  = 0;
	
	log_i("lib image init.\n");

	srand(platform_get_microsecond());
	
	file_list_create(&lib_image_list,CONFIG_PHOTO_PATH,MEDIA_TYPE_PHOTO,"./photofile.list");
	if(!lib_image_list){
		log_e("image list error\n");
		return -1;
	}
	
	ret=pthread_create(&lib_image_thread, NULL, lib_image_show_task, NULL);
	if(ret != 0){
		log_e("thread error=%s\n",strerror(ret));
		return -1;
	}

	ret=pthread_create(&lib_time_thread, NULL, lib_image_msg_task, NULL);
	if(ret != 0){
		log_e("thread error=%s\n",strerror(ret));
		return -1;
	}


	return 0;
}


int lib_image_deinit(void)
{

	



	return 0;
}

//****************************************************************

